﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class admin_AdminApply : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["studIdcard"] != null)
        {
            if (!Page.IsPostBack)
            {
                
                bindExamTimeList();
                bindDataTable();
            }
        }
    }

    private void bindExamTimeList()
    {
        DataTable dt = Exam.ExamTime_GetTopList();
        BSUtility.BindListControl(List_ExamAllName, dt, "EmAllName", "TimeID");

    }
    private void bindExamRank( GridViewEditEventArgs e,string examName)
    {
        string emId = gv.DataKeys[e.NewEditIndex].Values[3].ToString().Trim();
        DataTable dt = Exam.getRankNameByEmID(emId);
        DropDownList ddlRankList = (DropDownList)this.gv.Rows[e.NewEditIndex].FindControl("ddlRankList");
        BSUtility.BindListControl(ddlRankList, dt, "EmRankName", "");
        foreach (ListItem llt in ddlRankList.Items)
        {
            if (llt.Text == examName)
            {
                llt.Selected = true;
            }
        }
        
    }

    private void bindDataTable()
    {
        string timeID = List_ExamAllName.SelectedValue.ToString();
        if (timeID.Trim() != "")
        {
            string applyState = List_ApplyState.SelectedValue.ToString();

            DataTable dt = Exam.ApplyInfo_GetList(timeID);
            if (dt.Rows.Count > 0)
            {
                if (dt.Columns.Contains("ApplyStateCHN"))
                    dt.Columns.Remove("ApplyStateCHN");
                dt.Columns.Add("ApplyStateCHN");
                foreach (DataRow dr in dt.Rows)
                {
                    dr["ApplyStateCHN"] = int.Parse(dr["ApplyState"].ToString()) > 1 ? "已完成" : "未完成";
                }
                StringBuilder sb = new StringBuilder();
                sb.Append(" 1 = 1 ");
                if (applyState.Trim() != "-1")
                {
                    sb.Append(" And ApplyState='" + applyState.Trim() + "'");
                }
                if (txtStudentId.Text.Trim() != "")
                {
                    sb.Append(" And( StudIdcard like '%" + txtStudentId.Text.Trim() + "%' Or StudID like '%" + txtStudentId.Text.Trim() + "%')");
                }

                dt.DefaultView.RowFilter = sb.ToString();
                gv.DataSource = dt.DefaultView;
                gv.DataBind();
                gv.Visible = true;
            }
            else
            {
                gv.Visible = false;
            }
        }      
    }

    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        gv.PageIndex = e.NewPageIndex;
        bindDataTable();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        bindDataTable();       
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

       
        
    }
  
    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
        gv.EditIndex = e.NewEditIndex;
        string emRankName = this.gv.DataKeys[e.NewEditIndex].Values[6].ToString().Trim();
        bindDataTable();
        bindExamRank(e,emRankName);
    }
    protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //更新函数
        string applyId = gv.DataKeys[e.RowIndex].Values[0].ToString().Trim();
        string deptName = ((TextBox)this.gv.Rows[e.RowIndex].FindControl("txtDeptName")).Text.Trim();
        string className = ((TextBox)this.gv.Rows[e.RowIndex].FindControl("txtClassName")).Text.Trim();
        string rankName = ((DropDownList)this.gv.Rows[e.RowIndex].FindControl("ddlRankList")).Text.Trim();
        Exam.updateApplyInfoByApplyId(applyId,deptName,className,rankName);
        gv.EditIndex = -1;
        bindDataTable();
    }
    protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv.EditIndex = -1;
        bindDataTable();
    }
    protected void exportExcel_Click(object sender, EventArgs e)
    {
        string timeID = List_ExamAllName.SelectedValue.ToString();
        string applyState = List_ApplyState.SelectedValue.ToString();
        string examName = List_ExamAllName.SelectedItem.Text;
        DateTime time = DateTime.Now;

        if (timeID.Trim() != "")
        {
            DataTable dt = Exam.ApplyInfo_GetList(timeID);
            if (dt.Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(" 1 = 1 ");
                if (applyState.Trim() != "-1")
                {
                    sb.Append(" And ApplyState='" + applyState.Trim() + "'");
                }

                dt.DefaultView.RowFilter = sb.ToString();
                BSUtility.ExportDataTableToExcel(dt.DefaultView.ToTable(), examName + time.Year + "-" + time.Month + "-" + time.Day + applyState + "报名信息");
            }
            else
            {
                BSUtility.WindowAlert(Page,"没有内容");
            }
        }     

        
    }
}